package cn.ehanghai.iot.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * Created by allenwc on 16/5/16.
 */
public class TokenUtil {

    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String[] genTokenAndSecret() {
        String[] s = new String[2];
        s[0] = getRandomString(10);
        s[1] = getRandomString(10);
        return s;
    }

    public static Date getValidDate() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, 30);
        return c.getTime();
    }

    public static String[] genKeyAndSecret() {
        String[] s = new String[2];
        s[0] = getBigRandomString(16);
        s[1] = getBigRandomString(16);
        return s;
    }

    public static String getBigRandomString(int length) { //length表示生成字符串的长度
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

}
